package liangtWorkSpace.rsa;

import java.io.*;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @ClassName RSAGetKey
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/9 20:58
 */
public class RSAGetKey {

    public static void soreKey(String key, String keyName, String fileName) {
        Properties properties = new Properties();
        String path = null;

        try {
            path = "D:\\日志文件\\工作日志\\阿克苏缴费\\接口文档\\圈存机\\密钥\\";
            File file = new File(path + fileName);
            file.createNewFile();
            path = file.getAbsolutePath();
            System.out.println("文件地址："+path);
            try (OutputStream out = new FileOutputStream(file)) {
                properties.setProperty(keyName, key);
                properties.store(out, "This is " + keyName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取RSA公私钥信息
     *
     * @return Map<String   ,   String>{"privateKey":privateKey,"publicKey":publicKey}
     * @throws NoSuchAlgorithmException
     */
    public static Map getKey() throws NoSuchAlgorithmException {
        Map ketMap = new HashMap<>();
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSAConfig.getKeyAlgorithm());
        keyPairGenerator.initialize(512);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        ketMap.put(RSAConfig.getPublicKey(), getKey(publicKey));
        ketMap.put(RSAConfig.getPrivateKey(), getKey(privateKey));
        return ketMap;
    }

    /**
     * 将KEY进行解码
     *
     * @return
     */
    private static String getKey(Key key) {
        return encoder(key.getEncoded());
    }

    /**
     * 进行BASE64编码
     *
     * @param textByte
     * @return
     */
    private static String encoder(byte[] textByte) {
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(textByte);
    }

    /**
     * 获取RSA公私钥信息
     *
     * @param args
     */
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String privateKey =  String.valueOf(RSAGetKey.getKey().get(RSAConfig.getPrivateKey()));
        String publicKey = String.valueOf(RSAGetKey.getKey().get(RSAConfig.getPublicKey()));
        System.out.println("公钥信息:" + publicKey);
        System.out.println("私钥信息:" + privateKey);
        soreKey(publicKey,"publicKey","publicKey.properties");
        soreKey(privateKey,"privateKey","privateKey.properties");
    }
}
